<?php

	//$q = strtolower($_GET["id_rest"]);
	$q = $_POST['id_resto'];
	$type_graph = $_POST['type'];
	
	if (!$q) return;
	
	set_include_path('140dev/db');
	
	require_once('140dev_config.php');	  
	require_once('db_lib.php');
	
	$oDB = new db;
    
 	if(!$oDB)
  	{
  		die('Could not connect: ' . mysql_error());
  	}
	
	try{
		
		if($type_graph == "BY_WEB_REVIEWS"){ 
			$result = $oDB->select("SELECT ID_WEB_REVIEWS, AVG_SCORE_GLOBAL, DT_SCORE FROM HIST_SCORE_REVIEWS_DETAIL WHERE ID_RESTAURANT = " . $q);
			
			$return_search = "";
			while($row = mysqli_fetch_row($result))
			{
				$datetime = strtotime($row[2]);
				$dt_score = date("Y-m-d", $datetime);
				$scoring = $row[1];
				$web_review = $row[0];
				
				$return_search = $return_search . $web_review . ";" . $dt_score . ";" . $scoring . "|";
			}
			
			$return_search = substr_replace($return_search ,"",-1);
			
			echo $return_search;
		}
		if($type_graph == "TWITTER_FOLLOWERS"){
			$sql = "SELECT DATE_FORMAT(t3.DT_TIMESTAMP, '%Y-%m-%d'), ROUND(AVG(t3.NUM_FOLLOWERS)) FROM MAST_RESTAURANTS t1 ";
			$sql = $sql . "INNER JOIN MAST_TWITTER_ACCOUNTS t2 ON t1.DS_REST_TWITTER = t2.DS_SCREEN_NAME ";
			$sql = $sql . "INNER JOIN HIST_TWITTER_ACCOUNTS t3 ON t2.DS_SCREEN_NAME = t3.DS_SCREEN_NAME ";
			$sql = $sql . "WHERE t1.ID_RESTAURANT = '" . $q . "' GROUP BY DATE_FORMAT(t3.DT_TIMESTAMP, '%Y-%m-%d') ";
			$sql = $sql . "ORDER BY DT_TIMESTAMP ASC ";
			
			$result = $oDB->select($sql);
			
			$return_search = "";
			while($row = mysqli_fetch_row($result))
			{
				$datetime = strtotime($row[0]);
				$dt_update = date("Y-m-d", $datetime);
				$followers = $row[1];
				
				$return_search = $return_search . $dt_update . ";" . $followers . "|";
			}
			
			$return_search = substr_replace($return_search ,"",-1);
			
			echo $return_search;
		}
		if($type_graph == "FACEBOOK_LIKES"){

			$sql = "SELECT DATE_FORMAT(t3.DT_TIMESTAMP, '%Y-%m-%d'), ROUND(AVG(t3.NUM_LIKES)) FROM MAST_RESTAURANTS t1 ";
			$sql = $sql . "INNER JOIN MAST_FACEBOOK_ACCOUNTS t2 ON t1.DS_REST_FACEBOOK = t2.DS_USER_FB ";
			$sql = $sql . "INNER JOIN HIST_FACEBOOK_ACCOUNTS t3 ON t2.ID_USER_FB = t3.ID_USER_FB ";
			$sql = $sql . "WHERE t1.ID_RESTAURANT = '" . $q . "' GROUP BY DATE_FORMAT(t3.DT_TIMESTAMP, '%Y-%m-%d') ";
			$sql = $sql . "ORDER BY t3.DT_TIMESTAMP ASC ";
			
			$result = $oDB->select($sql);
			
			$return_search = "";
			while($row = mysqli_fetch_row($result))
			{
				$datetime = strtotime($row[0]);
				$dt_update = date("Y-m-d", $datetime);
				$likes = $row[1];
				
				$return_search = $return_search . $dt_update . ";" . $likes . "|";
			}
			
			$return_search = substr_replace($return_search ,"",-1);
			
			echo $return_search;
		}
		
		if($type_graph == "SCORING_BY_CATEGORIES"){

			$sql = "SELECT DT_SCORE, AVG_SCORE_CUISINE, AVG_SCORE_AMBIENT, AVG_SCORE_VALUE_PRICE, AVG_SCORE_SERVICE, ";
			$sql = $sql . " AVG_SCORE_WINE, AVG_SCORE_CLEAN, AVG_SCORE_NOISE ";
			$sql = $sql . "FROM HIST_SCORE_REVIEWS ";
			$sql = $sql . "WHERE ID_RESTAURANT = " . $q . " ";
			$sql =  $sql ."ORDER BY DT_SCORE DESC ";
			$sql = $sql . "LIMIT 1";
			
			
			$result = $oDB->select($sql);
			
			$return_search = "";
			while($row = mysqli_fetch_row($result))
			{
				$datetime = strtotime($row[0]);
				$dt_update = date("Y-m-d", $datetime);
				$cuisine = $row[1];
				$ambient = $row[2];
				$value_price = $row[3];
				$service = $row[4];
				$wine = $row[5];
				$clean = $row[6];
				$noise = $row[7];
				
				$return_search = $return_search . $dt_update . ";" . $cuisine . ";" . $ambient . ";" . $value_price . ";" . $service . ";" . $wine . ";" . $clean . ";" . $noise;
			}
			
			echo $return_search;
		}
	



	}
	catch(Exception $e){
		echo $e;
	}	

?>